﻿//Tzi Yong

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace Tzync
{
    public partial class RollbackUIForm : Form
    {
        private SyncTool syncTool;
        private Profile profile;
        private SystemType systemName;  
        private String rootAddress; //path of folder chosen by user
        private bool selectedAll;   //toggles select/de-select all

        public RollbackUIForm(SyncTool newSyncTool)
        {
            syncTool = newSyncTool;
            profile = syncTool.GetProfile();
            systemName = profile.SystemName;
            selectedAll = false;

            if (profile.SystemName == SystemType.Host)
                rootAddress = profile.RootAddressHost;
            else
                rootAddress = profile.RootAddressGuest;

            InitializeComponent();
        }

        private void RollbackUIForm_Load(object sender, EventArgs e)
        {
            infoLabel.Text = "";
            backBtnToolTip.SetToolTip(backBtn, "Click here to go back to Main");
            selectAllBtnToolTip.SetToolTip(selectAllBtn, "Click here to select or de-select all rollback files");
            rollbackBtnToolTip.SetToolTip(rollbackBtn, "Click here to restore the selected files");
            rbPreviewBtnToolTip.SetToolTip(previewCurrBtn, "Click here to view the current version of the selected files in a new window");
            rbPreviewPrevBtnToolTip.SetToolTip(previewPrevBtn, "Click here to view the rollback version of the selected files in a new window");
            helpBtnToolTip.SetToolTip(helpBtn, "Click here to download our help file online!");
            quitBtnToolTip.SetToolTip(quitBtn, "Why leave when you are having fun?");
            aboutBtnToolTip.SetToolTip(aboutBtn, "Click here to learn more about Tzync and check for updates!");

            DisplayRollback();
        }

        private void RollbackUIForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void tzyncLogoPB_Click(object sender, EventArgs e)
        {
            infoLabel.Text = "";
            Process.Start("http://code.google.com/p/tzync/");
        }

        private void backBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void selectAllBtn_Click(object sender, EventArgs e)
        {
            infoLabel.Text = "";
            if (rollbackLV.Items.Count > 0)
            {
                if (!selectedAll)
                {
                    foreach (ListViewItem item in rollbackLV.Items)
                        item.Checked = true;
                    selectedAll = true;
                }
                else
                {
                    foreach (ListViewItem item in rollbackLV.Items)
                        item.Checked = false;
                    selectedAll = false;
                }
            }
            else
                infoLabel.Text = "There are no files to rollback!";
        }

        private void rollbackBtn_Click(object sender, EventArgs e)
        {
            infoLabel.Text = "";

            if (rollbackLV.CheckedItems.Count == 0)
            {
                infoLabel.Text = "There are no files to rollback!";
                return;
            }

            Cursor.Current = Cursors.WaitCursor;
            infoLabel.Text = "Rollback in Progress";
            List<String> selectedRollbackList = new List<string>();
            foreach (ListViewItem item in rollbackLV.CheckedItems)
                selectedRollbackList.Add(item.SubItems[2].Text);    //list of selected files to rollback

            try
            {
                syncTool.PerformRollback(selectedRollbackList); //perform rollback
            }
            catch (FileNotFoundException f)
            {
                infoLabel.Text = "The folder " + f.FilePath + " is not found!";
                return;
            }
            catch (DirectoryNotFoundException d)
            {
                infoLabel.Text = "The folder " + d.FolderPath + " is not found!";
                return;
            }
            catch (UnauthorizedAccessException u)
            {
                infoLabel.Text = "The file " + u.Path + " cannot be accessed! Please try again.";
                return;
            }

            foreach (ListViewItem item in rollbackLV.CheckedItems)    //update listview
                item.Remove();

            Cursor.Current = Cursors.Default;
            infoLabel.Text = "Rollback complete!";
        }

        private void previewCurrBtn_Click(object sender, EventArgs e)
        {
            infoLabel.Text = "";
            if (rollbackLV.CheckedItems.Count == 0)
            {
                infoLabel.Text = "There are no files to preview!";
                return;
            }

            foreach (ListViewItem item in rollbackLV.CheckedItems)
            {
                if (File.Exists(item.Text) == true)
                    Process.Start(item.Text);   //preview selected files
                else
                    infoLabel.Text = "Currrent file does not exist and cannot be previewed!";
            }
        }

        private void previewPrevBtn_Click(object sender, EventArgs e)
        {
            infoLabel.Text = "";
            if (rollbackLV.CheckedItems.Count == 0)
            {
                infoLabel.Text = "There are no files to preview!";
                return;
            }
            foreach (ListViewItem item in rollbackLV.CheckedItems)
                Process.Start(item.SubItems[2].Text);   //preview selected files
        }

        private void quitBtn_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void helpBtn_Click(object sender, EventArgs e)
        {
            infoLabel.Text = "";
            Process.Start("http://tzync.googlecode.com/files/Tzync_User_Guide_V1.pdf");
        }

        //displays list of files for rollback
        private void DisplayRollback()
        {
            infoLabel.Text = "";
            rollbackSpaceLabel.Text = "Space Remaining/Allocated (MB): " + (syncTool.GetRollbackSpaceRemaining() / (1024 * 1024)) + " / " + (syncTool.GetRollbackSpaceAllocated() / (1024 * 1024));
            rollbackLV.Items.Clear();
            ListViewItem rollbackItems;
            List<RollbackFile> rollbackList = syncTool.GetRollbackFileList();   //retrieve list of rollback files
            if (rollbackList.Count > 0)
            {
                for (int i = rollbackList.Count - 1; i >= 0; i--)
                {
                    rollbackItems = new ListViewItem();
                    rollbackItems.Text = rollbackList[i].RbDestination;    //location where file will be rollback'ed to
                    rollbackItems.SubItems.Add("" + rollbackList[i].RbVersion); //version of file
                    rollbackItems.SubItems.Add("" + rollbackList[i].RbSource); //location where rollback file is stored
                    rollbackItems.SubItems.Add("" + rollbackList[i].RbLastSyncTime);   //date last synced of file
                    rollbackLV.Items.Add(rollbackItems);
                }
            }

            Utility.ResizeColumns(rollbackLV);
        }

        private void aboutBtn_Click(object sender, EventArgs e)
        {
            AboutUIForm aboutUI = new AboutUIForm();
            aboutUI.ShowDialog();
        }
    }
}
